package com.huashi.dealer.modular.business.setting.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huashi.dealer.commonEntity.SettingBanner;
import com.huashi.dealer.modular.business.setting.mapper.SettingBannerMapper;
import com.huashi.dealer.modular.business.setting.param.StoreBannerSaveParam;
import com.huashi.dealer.modular.business.setting.service.StoreSettingBannerService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StoreSettingBannerServiceImpl implements StoreSettingBannerService {

    @Resource
    private SettingBannerMapper settingBannerMapper;

    @Override
    public Page<SettingBanner> getBannerPage(Page<SettingBanner> page) {
        List<SettingBanner> list = settingBannerMapper.selectBannerWithImage(page);
        page.setRecords(list);
        return page;
    }

    @Override
    public void saveOrUpdateBanner(StoreBannerSaveParam param) {
        if (param.getImageId() == null || param.getImageId() == 0) {
            throw new IllegalArgumentException("请上传轮播图");
        }
        SettingBanner entity = new SettingBanner();
        entity.setId(param.getId());
        entity.setName(param.getName());
        entity.setImageId(param.getImageId());
        entity.setUrl(param.getUrl());
        entity.setStatus(param.getStatus() == null ? 10 : param.getStatus());
        long now = System.currentTimeMillis() / 1000;
        if (param.getId() == null) {
            entity.setCreateTime(now);
        } else {
            entity.setUpdateTime(now);
        }
        // BaseMapper 提供的saveOrUpdate风格，这里用insert/updateById
        if (param.getId() == null) {
            settingBannerMapper.insert(entity);
        } else {
            settingBannerMapper.updateById(entity);
        }
    }

    @Override
    public void deleteBanner(Long id) {
//        settingBannerMapper.deleteById( id)
        settingBannerMapper.softDeleteById(id);
    }
}


